package edu.iscas.appcheckclient.fragment;

import android.app.Fragment;
import android.app.ListFragment;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.iscas.appcheckclient.R;
import edu.iscas.appcheckclient.model.actions.Action;
import edu.iscas.appcheckclient.service.RecordService;

/**
 * Created by dell on 2016/1/12.
 */
public class ActionFragment extends android.support.v4.app.Fragment implements ListAdapter{

    Set<DataSetObserver> observerSet = new HashSet<>();
    protected TextView appInfoNameView;
    protected TextView appInfoPackageView;
    protected ImageView appIconView;

    protected List<Action> actionList;
    protected ListView interactionsListView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        this.actionList = RecordService.getActionList();

        View rootView = inflater.inflate(R.layout.fragment_action_list, container, false);
        interactionsListView = (ListView)rootView.findViewById(R.id.lv_interactions_info);
        this.interactionsListView.setAdapter(this);
        this.appInfoNameView = (TextView)rootView.findViewById(R.id.tv_appInfo_name);
        this.appInfoNameView.setText(args.getString("appName"));

        this.appInfoPackageView = (TextView)rootView.findViewById(R.id.tv_appInfo_package);
        this.appInfoPackageView.setText(args.getString("packageName"));

        this.appIconView = (ImageView)rootView.findViewById(R.id.iv_app_info_icon);
        this.appIconView.setImageDrawable(RecordService.getTestAppInfo().getIcon());

        return rootView;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        this.observerSet.add(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.observerSet.remove(observer);
    }

    @Override
    public int getCount() {
        return this.actionList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.actionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if((convertView != null) && (convertView instanceof LinearLayout)){
            ((LinearLayout) convertView).removeAllViews();
        }else{
            convertView = new LinearLayout(parent.getContext());
            ((LinearLayout)convertView).setOrientation(LinearLayout.VERTICAL);
        }
        Action action = this.actionList.get(position);
        TextView typeTextView = new TextView(parent.getContext());
        typeTextView.setText(action.getTypeString());
        ((LinearLayout)convertView).addView(typeTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        TextView valueTextView = new TextView(parent.getContext());
        valueTextView.setText(action.getValue());
        ((LinearLayout)convertView).addView(valueTextView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        if((position < 0) || (position >= this.actionList.size())){
            throw new ArrayIndexOutOfBoundsException(position);
        }
        return true;
    }
}
